package com.java.zhuiyun.api.book_mark.service.impl;

import com.java.zhuiyun.api.book_mark.entity.BookMark;
import com.java.zhuiyun.api.book_mark.mapper.BookMarkMapper;
import com.java.zhuiyun.api.book_mark.service.BookMarkService;
import com.java.zhuiyun.api.course_pc.entity.Course;
import com.java.zhuiyun.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BookMarkServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/31 10:26
 **/
@Service
public class BookMarkServiceImpl implements BookMarkService {

    @Autowired
    private BookMarkMapper bookMarkMapper;
    /*
    * @Author wuwan
    * @Description 课程收藏
    * @Date 2023/8/31 10:41
    * @Return 
    * @param null
    */
    @Override
    public CommonResult addBookMark(Integer uid, Integer cid) {
        List<BookMark> bookMark = bookMarkMapper.getBookMark(uid);
        for (int i = 0; i < bookMark.size(); i++) {
            if (bookMark.get(i).getCourseId().equals(cid)) {
                return CommonResult.error("已收藏此课程");
            }
        }
        Date createTime = new Date();
        Integer addBookmark = bookMarkMapper.addBookMark(uid, cid,createTime);
        if (addBookmark >= 1){
            return CommonResult.ok("收藏成功");
        }
        return CommonResult.error("收藏失败");
    }
    /*
    * @Author wuwan
    * @Description 查询收藏的课程
    * @Date 2023/8/31 10:41
    * @Return 
    * @param null
    */
    @Override
    public CommonResult getBookMark(Integer uid) {
        List<BookMark> bookMark = bookMarkMapper.getBookMark(uid);
        List<Course> list = new ArrayList<>();
        for (int i = 0; i < bookMark.size(); i++) {
            BookMark bookMark1 = bookMark.get(i);
            Course course = bookMarkMapper.getCourse(bookMark1.getCourseId());
            course.setBid(bookMark1.getId());
            list.add(course);
        }
        return CommonResult.ok(list);
    }
    /*
    * @Author wuwan
    * @Description 取消个人信息中课程收藏
    * @Date 2023/8/31 15:42
    * @Return
    * @param null
    */
    @Override
    public CommonResult delBookMark(Integer id) {
        Integer integer = bookMarkMapper.delBookMark(id);
        if (integer >= 1){
            return CommonResult.ok("已取消收藏");
        }
        return CommonResult.error("取消失败");
    }
    /*
     * @Author wuwan
     * @Description 取消课程收藏 //王旭用
     * @Date 2023/8/31 15:42
     * @Return
     * @param null
     */
    @Override
    public CommonResult delBookMarkCourse(Integer uid,Integer cid) {
        Integer integer = bookMarkMapper.delBookMarkCourse(uid,cid);
        if (integer >= 1){
            return CommonResult.ok("已取消收藏");
        }
        return CommonResult.error("取消失败");
    }
    /*
    * @Author wuwan
    * @Description 课程收藏与取消 王勃
    * @Date 2023/9/3 20:58
    * @Return
    * @param null
    */
    @Override
    public CommonResult getUser(Integer uid, Integer cid) {
        List<BookMark> user = bookMarkMapper.getUser(uid, cid);
        if (user.size() != 0){
            return CommonResult.ok(1);
        }
        return CommonResult.error(0);
    }
}
